package cn.stylefeng.guns.modular.cms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cms.entity.AdPosition;
import cn.stylefeng.guns.modular.cms.mapper.AdPositionMapper;
import cn.stylefeng.guns.modular.cms.model.params.AdPositionParam;
import cn.stylefeng.guns.modular.cms.model.result.AdPositionResult;
import  cn.stylefeng.guns.modular.cms.service.AdPositionService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 广告位表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2019-11-07
 */
@Service
public class AdPositionServiceImpl extends ServiceImpl<AdPositionMapper, AdPosition> implements AdPositionService {

    @Override
    public void add(AdPositionParam param){
        AdPosition entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(AdPositionParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(AdPositionParam param){
        AdPosition oldEntity = getOldEntity(param);
        AdPosition newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public AdPositionResult findBySpec(AdPositionParam param){
        return null;
    }

    @Override
    public List<AdPositionResult> findListBySpec(AdPositionParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(AdPositionParam param){
        //找出分类
        List<Map<String, Object>> list = this.baseMapper.customMapList(param);
        //用户id为空，则直接返回
        if (param.getApId() == null) {
        } else {
            //将相同的设置selected
            for (Map<String, Object> positionMap : list) {
                System.err.println(positionMap.get("id"));
                System.err.println(param.getApId().toString());
                if (param.getApId().intValue() ==(int)positionMap.get("apId")) {
                    positionMap.put("selected", true);
                }
            }
        }
        LayuiPageInfo layuiPageInfo = new LayuiPageInfo();
        layuiPageInfo.setData(list);
        return layuiPageInfo;
    }

    private Serializable getKey(AdPositionParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private AdPosition getOldEntity(AdPositionParam param) {
        return this.getById(getKey(param));
    }

    private AdPosition getEntity(AdPositionParam param) {
        AdPosition entity = new AdPosition();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
